﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Configuration;
using System.Net;
using System.Net.Sockets;
using _2QWebProxy.GUI;
using _2QWebProxy.App_Code;

namespace _2QWebProxy
{
    public partial class Main : Form
    {
        public List<Server> listServer = new List<Server>();
        public Thread ListenThread;
        public List<string> listPort;

        public Main()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
        }

        private void btnListen_Click(object sender, EventArgs e)
        {
            try
            {
                if (Properties.Settings.Default.isListen)
                {
                    listenToolStripMenuItem.Text = "Start";
                    toolStripStatusLabel.Text = "Disconnected";
                    foreach (var client in Server.clients)
                    {
                        client.Value.client.Shutdown(SocketShutdown.Both);
                        client.Value.client.Close();

                    }
                    for (int i = 0; i < listServer.Count; i++)
                    {
                        listServer[i].serverSocket.Close();
                        listServer[i].serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    }
                }
                else
                {
                    listenToolStripMenuItem.Text = "Stop";
                    ListenThread = new Thread(new ThreadStart(ServerListenThread));
                    ListenThread.Start();
                }
                Properties.Settings.Default.isListen = !Properties.Settings.Default.isListen;
                //ServerListenThread();
            } catch 
            {
            }
        }

        private void ServerListenThread()
        {
            if (Properties.Settings.Default.isListen)
            {
                listServer.Clear();
                List<Port> listPort = Utils.GetPortList();
                foreach (var port in listPort)
                {
                    Server server = new Server();
                    server.port = port.PortNumber;
                    listServer.Add(server);
                }
                if (listServer.Count > 0)
                {
                    //listenToolStripMenuItem.Enabled = false;
                    toolStripStatusLabel.Text = "Listening at port ";
                    for (int i = 0; i < listServer.Count; i++)
                    {
                        if (listServer[i].Listen())
                        {
                            toolStripStatusLabel.Text += listServer[i].port + ", ";
                            listServer[i].serverSocket.BeginAccept(AcceptClient, null);
                        }
                        else
                        {
                            toolStripStatusLabel.Text = "Unable to listen at port " + listServer[i].port;
                            listenToolStripMenuItem.Enabled = true;
                        }
                    }
                    toolStripStatusLabel.Text = toolStripStatusLabel.Text.Substring(0, toolStripStatusLabel.Text.Length - 2);
                }
                else
                {
                    MessageBox.Show("Missing port number");
                    listenToolStripMenuItem.Enabled = true;
                }
            }
        }

        private void AcceptClient(IAsyncResult ar)
        {
            if (Properties.Settings.Default.isListen)
            {
                for (int i = 0; i < listServer.Count; i++)
                {
                    try
                    {
                        CClient client = new CClient(listServer[i].serverSocket.EndAccept(ar));
                        Server.clients.Add(client.guid.ToString(), client);

                        listServer[i].serverSocket.BeginAccept(AcceptClient, null);
                    }
                    catch (Exception se)
                    {
                        MessageBox.Show(se.Message + se.StackTrace.ToString());
                    }
                }
            }
        }
        private void txtText_TextChanged(object sender, EventArgs e)
        {

        }

        private void portToolStripMenuItem_Click(object sender, EventArgs e)
        {
            panelMain.Controls.Clear();
            UserControl uc = new ucPortList();
            panelMain.Controls.Add(uc);
        }

        private void ruleSettingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            panelMain.Controls.Clear();
            UserControl uc = new ucRuleSetting();
            panelMain.Controls.Add(uc);
        }

        private void chainProxyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            panelMain.Controls.Clear();
            UserControl uc = new ucProxyList();
            panelMain.Controls.Add(uc);
        }

    }
}
